package bdsimple.impl.mysql;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import bdsimple.abstractfactory.AbstractFactoryDAO;
import bdsimple.dao.base.FabricaDAO;
import bdsimple.dao.base.PropiedadDAO;
import bdsimple.dto.base.FabricaDTO;
import bdsimple.dto.base.PropiedadDTO;
import dao.base.api.IDAO;
import dao.base.api.IDTO;

public class FabricaDAOImpl extends MySQLBaseDAO implements FabricaDAO {

	public FabricaDAOImpl() {
		super(FabricaDTO.class);
		daoParentClass = PropiedadDAO.class;
	}
	
	@Override
	protected String createTableColumns() throws Exception {
		StringBuffer strbuf = new StringBuffer();

	    strbuf.append(FabricaDTOImpl.ID);
	    strbuf.append(" INT PRIMARY KEY, ");
	    strbuf.append(FabricaDTOImpl.PUNTOS_EXPERIENCIA);
	    strbuf.append(" INT ");
	    
	    return strbuf.toString();
	}

	@Override
	protected String createInsertCollst(IDTO dto) throws Exception {

		StringBuffer strbuf = new StringBuffer();

		strbuf.append(FabricaDTOImpl.ID);
		strbuf.append(", ");
		strbuf.append(FabricaDTOImpl.PUNTOS_EXPERIENCIA);

		return strbuf.toString();

	}

	@Override
	protected String createInsertValues(IDTO dto) throws Exception {
		
		FabricaDTOImpl fabricaDTOImpl = (FabricaDTOImpl) dto;

		StringBuffer strbuf = new StringBuffer();
		strbuf.append(fabricaDTOImpl.getId());
	    strbuf.append(", ");
		strbuf.append(fabricaDTOImpl.getPuntosExperiencia());

		return strbuf.toString();
		
	}



	@Override
	protected String createUpdateValues(IDTO dto) throws Exception {
		
		FabricaDTOImpl fabricaDTOImpl = (FabricaDTOImpl) dto;

		StringBuffer strbuf = new StringBuffer();

		strbuf.append(FabricaDTOImpl.PUNTOS_EXPERIENCIA);
		strbuf.append(" = ");
		strbuf.append(fabricaDTOImpl.getPuntosExperiencia());

		return strbuf.toString();
		
	}
	
	public List<IDTO> listAll(int lim, int off) throws Exception {

	    StringBuffer strbuf = new StringBuffer();

	    IDAO par0 = //
	    AbstractFactoryDAO.getFactoryDAO().getDAO( //
	        daoParentClass, connectionBean);

	    strbuf.append("SELECT * FROM ");
	    strbuf.append(this.getTableName());
	    strbuf.append(", ");
	    strbuf.append(par0.getTableName());

	    strbuf.append(" WHERE ");
	    strbuf.append( //
	        /**/par0.getTableName() + "." + PropiedadDTO.ID + //
	            " = " + //
	            this.getTableName() + "." + FabricaDTO.ID);

	    if (lim >= 0 && off >= 0) {
	      strbuf.append(" LIMIT  ");
	      strbuf.append(lim);
	      strbuf.append(" OFFSET ");
	      strbuf.append(off);
	    }

	    System.err.println(strbuf.toString());

	    ResultSet rs = //
	    connection.createStatement().executeQuery(strbuf.toString());

	    List<IDTO> ret = new ArrayList<IDTO>();

	    while (rs.next()) {
	      ret.add(resultSetToDTO(rs));
	    }

	    return ret;
	  }
	
	public List<IDTO> listBy(String key, Object val) throws Exception {

	    if (key == null || val == null) {
	      throw new IllegalArgumentException("key == null || val == null");
	    }

	    StringBuffer strbuf = new StringBuffer();

	    IDAO par0 = //
	    AbstractFactoryDAO.getFactoryDAO().getDAO( //
	        daoParentClass, connectionBean);

	    strbuf.append("SELECT * FROM ");
	    strbuf.append(this.getTableName());
	    strbuf.append(", ");
	    strbuf.append(par0.getTableName());

	    strbuf.append(" WHERE ");
	    strbuf.append(key);
	    strbuf.append(" = ");
	    strbuf.append(val);

	    strbuf.append(" AND ");
	    strbuf.append( //
	        /**/par0.getTableName() + "." + PropiedadDTO.ID + //
	            " = " + //
	            this.getTableName() + "." + FabricaDTO.ID);

	    System.err.println(strbuf.toString());

	    ResultSet rs = //
	    connection.createStatement().executeQuery(strbuf.toString());

	    List<IDTO> ret = new ArrayList<IDTO>();

	    while (rs.next()) {
	      ret.add(resultSetToDTO(rs));
	    }

	    return ret;
	  }
	


	@Override
	protected IDTO internalResultSetToDTO(ResultSet rs, IDTO dto)
			throws Exception {
		
		FabricaDTOImpl ret = (FabricaDTOImpl) dto;
		
		ret.setPuntosExperiencia(rs.getInt(FabricaDTOImpl.PUNTOS_EXPERIENCIA));
	    
	    return ret;
	    
	}
	
	public byte[] loadImageByPropiedadId(int propiedadId) throws SQLException, Exception {
        StringBuffer strbuf = new StringBuffer();

        strbuf.append("SELECT ");
        strbuf.append(PropiedadDTO.IMAGEN);
        strbuf.append(" FROM ");
        strbuf.append(getTableName());
        strbuf.append(" WHERE ");
        strbuf.append(PropiedadDTO.ID);
        strbuf.append(" = ");
        strbuf.append(propiedadId);

        System.err.println(strbuf.toString());

        ResultSet rs = //
            connection.createStatement().executeQuery(strbuf.toString());
        
        if(rs.next()) {
            return rs.getBytes(PropiedadDTO.IMAGEN);
        }
        
        return null;
    }

}
